//
//  UIViewController+UINavigation.m
//  导航栏定制
//
//  Created by LG on 2017/11/23.
//  Copyright © 2017年 my. All rights reserved.
//

#import "UIViewController+Example.h"
#import <objc/runtime.h>
#import "Masonry.h"
#import "MPFNavigationBarView.h"

static void *NavigationBarKey_example = @"NavigationBarKey_example";
static void *ISNavigationBarShowingKey_example = @"ISNavigationBarShowingKey_example";
static void *DefaultNavigationCenterItemKey_example = @"DefaultNavigationCenterItemKey_example";
static void *NavigationBarTypeKey_example = @"NavigationBarTypeKey_example";

#define MPT_Device_Is_iPhoneX ([UIScreen instancesRespondToSelector:@selector(currentMode)] ?\
CGSizeEqualToSize(CGSizeMake(1125, 2436), [[UIScreen mainScreen] currentMode].size) : NO)


@interface UIViewController ()
<
MPFNavigationBarDelegate
>

@property (nonatomic, assign) MPFNavigationBarViewType_example navigationBarType_example;
@property (nonatomic, strong) MPFNavigationCenterItem *defaultNavigationCenterItem_example;

@property (nonatomic, assign) BOOL isNavigationBarShowing_example;

@end

@implementation UIViewController (Example)

#pragma mark - Inherit

#pragma mark - Public

- (CGFloat)navigationBarHeight_example {
    CGFloat height = [self.navigationBar_custom_example statusBarHeight_inner];
    if (self.isNavigationBarShowing_example) {
        height += 44.;
    }
    
    return height;
}


- (void)hiddenNavigationBar_example:(BOOL)hidden {
    self.isNavigationBarShowing_example = !hidden;
}

//TODO:隐藏导航栏下方的线
- (void)hiddenNavigationbarBottomLine_example:(BOOL)hidden
{
    self.navigationBar_custom_example.bottomLineView.hidden = hidden;
}

- (void)setCustomNavigationTitle_example:(MPFNavigationCenterItem*)titleItem {
    if (![titleItem isKindOfClass:[MPFNavigationCenterItem class]]) {
        return;
    }
    
    self.isNavigationBarShowing_example = YES;
    self.navigationBarType = MPFNavigationBarViewTypeCustom_example;
    [self.navigationBar_custom_example updateCenterNavigationItem:titleItem];
}

- (void)setNavigationTitle_example:(NSString*)title  {
    [self setNaviTitle_example:title withDefaultBack:NO handler:nil];
}

- (void)setNaviTitle_example:(NSString*)title withDefaultBack:(BOOL)defaultBack handler:(MPFNavigationAction)handler {
    self.isNavigationBarShowing_example = YES;
    
    UILabel *label = (UILabel *)self.defaultNavigationCenterItem_example.centerView;
    label.text = title;
    if (self.navigationBarType_example == MPFNavigationBarViewTypeCustom_example){
        [self initDefaultNavigationCenterItem_example];
    }
    
    if (defaultBack) {
        UIImage *img = [UIImage imageNamed:KMPFNavigationBarDefaultBackImageName_example];
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setImage:img forState:UIControlStateNormal];
        btn.frame = CGRectMake(0, 0, 44., 44.);
        
        MPFNavigationItem *item = [MPFNavigationItem actionWithContentView:btn info:nil handler:^(NSDictionary *info) {
            if (handler){
                handler(info);
            }
        }];
        
        [self addNavigationBarViewWithLeftButton_example:@[item]];
    }
    
    self.navigationBarType_example = MPFNavigationBarViewTypeDefault_example;
}

- (void)addNavigationBarViewWithLeftButton_example:(NSArray*)leftItems {
    self.isNavigationBarShowing_example = YES;
    
    self.navigationBar_custom_example.leftItems = leftItems;
}

- (void)addNavigationBarViewWithRightButton_example:(NSArray*)rightItems {
    self.isNavigationBarShowing_example = YES;
    
    self.navigationBar_custom_example.rightItems = rightItems;
}

#pragma mark - Private Methods

- (void)initNavigationBar_example:(BOOL)needNavigationBar {
    if (needNavigationBar) {
        if (!self.navigationBar_custom_example) {
            MPFNavigationCenterItem *item = [MPFNavigationCenterItem navigationCenterItemView:[[UIView alloc]init] size:CGSizeZero];
            self.navigationBar_custom_example = [[MPFNavigationBarView alloc]initWithCenterNavigationItem:item delegate:self];
            CGFloat height = [self.navigationBar_custom_example statusBarHeight_inner] + 44.;
            self.navigationBar_custom_example.frame = CGRectMake(0, 0, CGRectGetWidth([UIScreen mainScreen].bounds), height);
            [self.view addSubview:self.navigationBar_custom_example];
            
            //设置默认的导航栏标题视图
            [self initDefaultNavigationCenterItem_example];
        }
        self.navigationBar_custom_example.hidden = NO;
    } else {
        self.navigationBar_custom_example.hidden = YES;
    }
}


/**
 设置默认的导航栏标题视图
 */
- (void)initDefaultNavigationCenterItem_example {
    if (!self.defaultNavigationCenterItem_example) {
        UILabel *label = [[UILabel alloc]init];
        label.textAlignment = NSTextAlignmentCenter;
        label.font = KMPFNavigationBarDefaultTitleFont_example;
        label.textColor = KMPFNavigationBarDefaultTitleColor_example;
        [label sizeToFit];
        [label setBackgroundColor:[UIColor clearColor]];
        self.defaultNavigationCenterItem_example = [MPFNavigationCenterItem navigationCenterItemView:label size:CGSizeZero];
    }
    
    [self.navigationBar_custom_example updateCenterNavigationItem:self.defaultNavigationCenterItem_example];
    self.navigationBarType_example = MPFNavigationBarViewTypeDefault_example;
}


#pragma mark - Actions

#pragma mark - Delegate

#pragma mark - Data

#pragma mark - Notification

#pragma mark - Getter/Setter

- (void)setNavigationBar_custom_example:(MPFNavigationBarView *)navigationBar {
    objc_setAssociatedObject(self, NavigationBarKey_example, navigationBar, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)setIsNavigationBarShowing_example:(BOOL)isNavigationBarShowing {
    objc_setAssociatedObject(self, ISNavigationBarShowingKey_example, @(isNavigationBarShowing), OBJC_ASSOCIATION_ASSIGN);
    [self initNavigationBar_example:isNavigationBarShowing];
}

- (void)setDefaultNavigationCenterItem_example:(MPFNavigationCenterItem *)defaultNavigationCenterItem {
    objc_setAssociatedObject(self, DefaultNavigationCenterItemKey_example, defaultNavigationCenterItem, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)setNavigationBarType:(MPFNavigationBarViewType_example)navigationBarType {
    objc_setAssociatedObject(self, NavigationBarTypeKey_example, @(navigationBarType), OBJC_ASSOCIATION_ASSIGN);
}

- (MPFNavigationBarView *)navigationBar_custom_example {
    return objc_getAssociatedObject(self, NavigationBarKey_example);
}

- (BOOL)isNavigationBarShowing_example {
     return [objc_getAssociatedObject(self, ISNavigationBarShowingKey_example) boolValue];
}

- (MPFNavigationCenterItem *)defaultNavigationCenterItem_example {
    return objc_getAssociatedObject(self, DefaultNavigationCenterItemKey_example);
}

- (MPFNavigationBarViewType_example)navigationBarType {
    return [objc_getAssociatedObject(self, NavigationBarTypeKey_example)integerValue];
}

@end
